/**
 * 
 */
package info.niwota.fwrite;

import java.util.Date;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author qiangli
 *
 */
public class SessionListAdapter extends BaseAdapter {
	protected static class ViewHolder {
		public ImageView image1;
		public TextView text1;
		public TextView text2;
		public ImageView image2;
	}

	protected Context ctx;
	
	protected SessionInfo[] sessions;
	
	public SessionListAdapter(Context ctx, SessionInfo[] sessions) {
		this.ctx = ctx;
		this.sessions = sessions;
	}
	
	public int getCount() {
		return sessions == null ? 0 : sessions.length;
	}

	public SessionInfo getItem(int pos) {
		return sessions == null ? null : sessions[pos];
	}

	public long getItemId(int pos) {
		return pos;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			LayoutInflater li = LayoutInflater.from(ctx);
			convertView = li.inflate(R.layout.sess_list_item, null);
			holder = new ViewHolder();

			holder.text1 = (TextView) convertView.findViewById(R.id.item_text1);
			holder.text2 = (TextView) convertView.findViewById(R.id.item_text2);
			holder.image1 = (ImageView) convertView.findViewById(R.id.item_image1);
			holder.image2 = (ImageView) convertView.findViewById(R.id.item_image2);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		SessionInfo item = getItem(position);
		
		if (item != null) {
			holder.text1.setText(item.peer);
			holder.text2.setText(new Date(item.date).toLocaleString());
			holder.image2.setImageResource(item.hasMessage ? R.drawable.alert16 : R.drawable.empty);
		}
		return convertView;
	}
}
